jQuery Select Value |
您所在的位置:网站首页 › jquery to get selected value from dropdown › jQuery Select Value |
Updated April 19, 2023 jQuery select value lets you get the value of the selected option from the select boxes and dropdowns. jQuery offers two methods, val() and text()to achieve this. jQuery: selected selector is used in combination with the val() or text() method to find the value for the selected option in a select box or dropdown. Using val() method, we get the value to be sent to the server and with text() method, we get the text value of the selected element. Start Your Free Software Development Course Web development, programming languages, Software testing & others Syntax: 1. Using val() method: val() method is used to return the value of the selected element $("#selectedId option : selected").val()2. Using text() method: text() method is used to return the text value (string) of the selected element $("#selectedId option : selected").text()where,val() and text() do not accept any arguments. How does jQuery Select Value Work?Select elements basically have two values that can be accessed. The first value is to be sent to the server, which we can get using val() The second value is the text value of the selected element which we can get using text() method. EnglishWhere, books is the value returned by val() method. Englishis the value returned by text() method. Whenever an items gets selected in the dropdown, jQuery OnChange event handler gets executed where val() or text() method fetches the value of the selected item and then perform various operations over them. Examples for jQuery Select ValueLet us go through few examples to understand how to get the select values from select boxes using jQuery. Example #1This example demonstrates how jQuery select value actually works with val() method. Code: jQuery Get Selected Option Value $(document).ready(function () { $("#SubjectsList").change(function () { var selectedSubject = $("#SubjectsList option:selected").val(); alert("You have selected the country - " + selectedSubject); }); }); #divstyle { width: 500px; height: 250px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select value using val() method Select: English Physics Chemistry MathematicsOutput: Below screen displays when the above code gets executed. By default, we have the selected option as “English” in the given dropdown.This example demonstrates how jQuery select value actually works with text() method. Code: jQuery Get Selected Option Value $(document).ready(function () { $("#SubjectsList").change(function () { var selectedSubject = $("#SubjectsList option:selected").text(); alert("You have selected the country - " + selectedSubject); }); }); #divstyle { width: 500px; height: 250px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select value using text() method Select: English Physics Chemistry MathematicsOutput: Below screen displays when the above code gets executed. By default, the selected option is “English” in the given dropdown.This example demonstrates how the selected values can be retrieved from multiple select boxes using jQuery. Code: jQuery select values from Multiple Select Box $(document).ready(function () { $("button").click(function () { var multipleValues = $( "#subj" ).val() || []; $( "p" ).html(" You selected: " + multipleValues.join( ", " ) ); }); }); #divstyle { width: 500px; height: 350px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select values from Multiple Select Box English Maths Physics Chemistry History Geography ClickOutput: Below screen displays when the code above gets executed. We can see a multiple select box which allows users to make multiple selections.Given below is another similar example that demonstrates how the selected values can be retrieved from multiple select boxes using jQuery. Code: jQuery select values from Multiple Select Box $(document).ready(function () { $("button").click(function () { var selectedValues = []; $.each($("#subj option:selected"), function () { selectedValues.push($(this).val()); }); alert("Your favorite website is - " + selectedValues.join(", ")); }); }); #divstyle { width: 500px; height: 350px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select values from Multiple Select Box English Maths Physics Chemistry History Geography ClickOutput: Below screen displays when the code above gets executed.This is a guide to jQuery Select Value. Here we also discuss the introduction and how does jquery select value work? along with different examples and its code implementation. You may also have a look at the following articles to learn more – jQuery Widgets JQuery Modal jQuery Reset Form jQuery Slider GOLANG Course Bundle - 6 Courses in 1 23+ Hours of HD Videos 6 Courses Verifiable Certificate of Completion Lifetime Access 4.5 iOS DEVELOPER Course Bundle - 61 Courses in 1 147+ Hours of HD Videos 61 Courses Verifiable Certificate of Completion Lifetime Access 4.5 JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests 56+ Hours of HD Videos 18 Courses 6 Mock Tests & Quizzes Verifiable Certificate of Completion Lifetime Access 4.5 RED HAT LINUX Course Bundle - 5 Courses in 1 28+ Hours of HD Videos 5 Courses Verifiable Certificate of Completion Lifetime Access 4.5 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |